﻿// -----------------------------------------------------------------------
// <copyright file="RSACrytography.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
using System.Security.Cryptography;
using System.IO;
using System;

namespace EContractServer.SecurityLibrary
{

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RSACrytography
    {
        // Properties
        private RSACryptoServiceProvider m_rsaProvider;

        /// <method>Constructor</method>
        public RSACrytography()
        {
        }


        public RSACrytography( string p_sPublicKey, string p_sPrivatePublicKey )
        {

        }

        //public RSACrytography( string p_sFilePathPublicKey, string p_sFilePathPrivatePublicKey )
        //{

        //}

        private void AssignParameter()
        {
            const int PROVIDER_RSA_FULL = 1;
            const string CONTAINER_NAME = "SpiderContainer";
            CspParameters cspParams = new CspParameters( PROVIDER_RSA_FULL );
            cspParams.KeyContainerName = CONTAINER_NAME;
            cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
            cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
            m_rsaProvider = new RSACryptoServiceProvider( cspParams );
        }

        // Generate private/public key
        public string GenerateKey( int p_iKeySize, bool p_bIsPublicPrivateKey, bool p_bIsSaved )
        {
            string l_sKey = string.Empty;
            RSACryptoServiceProvider l_rsaProvider = new RSACryptoServiceProvider( p_iKeySize );
            string l_sKeyFile = @"D:\test\Crytography\key.xml";
            string l_sPublicKeyFile = @"D:\test\Crytography\publickey.xml";

            l_sKey = l_rsaProvider.ToXmlString( p_bIsPublicPrivateKey );

            // Write to file
            if ( !string.IsNullOrWhiteSpace( l_sKey ) && p_bIsSaved )
            {
                if ( p_bIsPublicPrivateKey )
                {
                    Utility.WriteFile( l_sKeyFile, "<BitStrength>" + p_iKeySize.ToString() + "</BitStrength>" + l_sKey );
                }

                l_sKey = l_rsaProvider.ToXmlString( false );
                Utility.WriteFile( l_sPublicKeyFile, "<BitStrength>" + p_iKeySize.ToString() + "</BitStrength>" + l_sKey );
            }

            return l_sKey;
        }

        // Encrypt
        public string EncryptString( string p_sPublicKey, string p_sOriginalString )
        {
            AssignParameter();

            string l_sEncryptString = string.Empty;
            m_rsaProvider.FromXmlString( p_sPublicKey );

            byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes( p_sOriginalString );
            byte[] cipherbytes = m_rsaProvider.Encrypt( plainbytes, true );
            l_sEncryptString = Convert.ToBase64String( cipherbytes );
            return l_sEncryptString;
        }

        // Decrypt
        public string DecryptString( string p_sKey, string p_sCypher )
        {
            AssignParameter();

            string l_sDecryptString = string.Empty;
            m_rsaProvider.FromXmlString( p_sKey );

            byte[] l_arrBase64String = Convert.FromBase64String( p_sCypher );
            byte[] l_arrPlainBytes = m_rsaProvider.Decrypt( l_arrBase64String, false );
            l_sDecryptString = System.Text.Encoding.UTF8.GetString( l_arrPlainBytes );
            return l_sDecryptString;
        }

        // Encrypt
        public string EncryptByteNumber( string p_sPublicKey, byte[] p_sPlainBytes )
        {
            AssignParameter();

            string l_sEncryptString = string.Empty;
            m_rsaProvider.FromXmlString( p_sPublicKey );

            byte[] cipherbytes = m_rsaProvider.Encrypt( p_sPlainBytes, true );
            l_sEncryptString = Convert.ToBase64String( cipherbytes );
            return l_sEncryptString;
        }

        // Decrypt
        public string DecryptByteNumber( string p_sKey, byte[] p_sCypherByte )
        {
            AssignParameter();

            string l_sDecryptString = string.Empty;
            m_rsaProvider.FromXmlString( p_sKey );

            byte[] l_arrPlainBytes = m_rsaProvider.Decrypt( p_sCypherByte, false );
            l_sDecryptString = System.Text.Encoding.UTF8.GetString( l_arrPlainBytes );
            return l_sDecryptString;
        }

        ////utility
        //private bool saveFile( string title, string filterString, string outputString )
        //{
        //    saveFileDialog.Title = title;
        //    saveFileDialog.Filter = filterString;
        //    saveFileDialog.FileName = "";
        //    if ( saveFileDialog.ShowDialog() == DialogResult.OK )
        //    {
        //        try
        //        {
        //            StreamWriter streamWriter = new StreamWriter( saveFileDialog.FileName, false );
        //            if ( outputString != null )
        //            { streamWriter.Write( outputString ); }
        //            streamWriter.Close();
        //            return true;
        //        }
        //        catch ( Exception Ex )
        //        {
        //            Console.WriteLine( Ex.Message );
        //            return false;
        //        }
        //    }
        //    return false;
        //}
    }
}
 